home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / PopUpFormatter / PopupFormatter.m < prev    next >
Text File  |  1995-06-12  |  4KB  |  150 lines

  1.  
  2. #import "PopupFormatter.h"
  3.  
  4. #define _delWill1    @selector(formatterWillChangeValueFor:at:sender:)
  5. #define _delWill2    @selector(formatterWillChangeValueFor:at:to:sender:)
  6. #define _delDid    @selector(formatterDidChangeValueFor:at:to:sender:)
  7. #define String char*
  8.  
  9. char *unitOfMeasurement[] = {
  10.     "barrels",
  11.     "gallons",
  12.     "litres",
  13.     "inches",
  14.     "centimeters",
  15.     "kilometers" ,
  16.     NULL
  17. };
  18.  
  19. @implementation PopupFormatter
  20.  
  21. - init
  22. {
  23.     [super init];
  24.         newValue = [[DBValue allocFromZone:[self zone]] init];
  25.         return self;
  26. }
  27.  
  28. - mouseDown:(NXEvent *)theEvent at:(int)row :(int)column
  29.     inside:(NXRect *)frame inView:(View *) view
  30.     withAttributes:(id <DBTableVectors >)rowAttrs
  31.      :(id <DBTableVectors >)columnAttrs
  32.     usePositions:(BOOL)useRowPos :(BOOL)useColumnPos
  33. {
  34.     String* strings;
  35.     int    x;
  36.  
  37.     [self getValueAt:row :column withAttributes:rowAttrs :columnAttrs
  38.         usePositions:useRowPos :useColumnPos];
  39.  
  40.  
  41.     popupList = [[PopUpList alloc] init];
  42.  
  43.  
  44.     for (strings = unitOfMeasurement; *strings; strings++)
  45.     {
  46.         [popupList addItem: *strings];
  47.     }
  48.  
  49.     popupButton = NXCreatePopUpListButton(popupList);
  50.     [popupButton setFrame: frame];
  51.  
  52.     for (x = 0; x < [[[popupButton target] itemList]cellCount]; x++)
  53.     {
  54.         if (!strcmp([[[[popupButton target] itemList] cellAt: x: 0] title], [value stringValue]))
  55.             {
  56.             [[[popupButton target] itemList] selectCellAt:x :0];
  57.             [popupButton setTitle:[[[[popupButton target] itemList] selectedCell]title]];
  58.             break;
  59.             }
  60.     }
  61.  
  62.     [view addSubview:popupButton];
  63.  
  64.     [popupList popUp: popupButton];
  65.  
  66.     [newValue setStringValue: [popupList selectedItem]];
  67.  
  68.     [popupButton removeFromSuperview];
  69.     [popupList free];
  70.     [popupButton free];
  71.  
  72.     [self setValueAt:row :column
  73.         withAttributes:rowAttrs :columnAttrs
  74.         usePositions:useRowPos :useColumnPos];
  75.     
  76.     /* Redraw the modified field */
  77.     [self drawFieldAt:row :column
  78.         inside:frame inView:view
  79.         withAttributes:rowAttrs :columnAttrs
  80.         usePositions:useRowPos :useColumnPos];
  81.  
  82.     [view display];
  83.  
  84.     return self;
  85. }
  86.  
  87.  
  88. - setValueAt:(int)row :(int)column
  89.     withAttributes:(id <DBTableVectors >)rowAttrs
  90.      :(id <DBTableVectors >)columnAttrs
  91.     usePositions:(BOOL)useRowPos :(BOOL)useColumnPos
  92. {
  93.     BOOL                isChangeOk = YES;
  94.  
  95.  /* Ask delegate if we can change */
  96.     if (useRowPos && !useColumnPos && delWill2)
  97.     isChangeOk = [delegate formatterWillChangeValueFor:
  98.               [columnAttrs identifier]
  99.               at:row to:newValue sender:self];
  100.     else if (useColumnPos && !useRowPos && delWill2)
  101.     isChangeOk = [delegate formatterWillChangeValueFor:
  102.               [rowAttrs identifier]
  103.               at:column to:newValue sender:self];
  104.     if (!isChangeOk)
  105.     return nil;
  106.  
  107.  /* Tell dataSource our newValue */
  108.     if (useRowPos && !useColumnPos)
  109.     [dataSource setValueFor:[columnAttrs identifier] at:row
  110.      from:newValue];
  111.     else if (useColumnPos && !useRowPos)
  112.     [dataSource setValueFor:[rowAttrs identifier] at:column
  113.      from:newValue];
  114.    
  115.  /* Tell delegate we changed */
  116.     if (useRowPos && !useColumnPos && delDid)
  117.     [delegate formatterDidChangeValueFor:[columnAttrs identifier]
  118.      at:row to:newValue sender:self];
  119.     else if (!useRowPos && useColumnPos && delDid)
  120.     [delegate formatterDidChangeValueFor:[rowAttrs identifier]
  121.      at:column to:newValue sender:self];
  122.     return self;
  123. }
  124.  
  125. - setDelegateFlags
  126. {
  127.     delWill1 = delWill2 = delDid = NO;
  128.  
  129.     if ([delegate respondsTo:_delWill1])
  130.     delWill1 = YES;
  131.     if ([delegate respondsTo:_delWill2])
  132.     delWill2 = YES;
  133.     if ([delegate respondsTo:_delDid])
  134.     delDid = YES;
  135.     return self;
  136. }
  137.  
  138. - setDelegate:newDelegate
  139. {
  140.     if (newDelegate == delegate)
  141.     return self;
  142.     else
  143.     [super setDelegate:newDelegate];
  144.  
  145.     [self setDelegateFlags];
  146.  
  147.     return self;
  148. }
  149.  
  150. @end